package com.mioto.ld.module.device.controller;

import com.mioto.ld.module.alert.statistic.DeviceAlertStatisticsFactory;
import com.mioto.ld.module.device.model.DeviceAlertLogAppPagerVO;
import com.mioto.ld.module.device.model.DeviceAlertLogPagerVO;
import com.mioto.ld.module.device.model.DeviceAlertStatisticsVO;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.web.bind.annotation.*;
import com.mioto.ld.module.device.service.IDeviceAlertLogService;
import com.mioto.ld.result.ResultData;
import com.mioto.ld.component.BasePager;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author mioto
 * @date 2024-05-15 16:13:50
 */
@RestController
@RequestMapping("deviceAlertLog")
@Api(tags = "设备告警记录管理")
public class DeviceAlertLogController {
    @Resource
    private IDeviceAlertLogService deviceAlertLogService;

    @GetMapping("/pager")
    @ApiOperation(value="web分页查询设备告警记录",response = DeviceAlertLogPagerVO.class)
    public ResultData pager (Integer deviceId,String startTime,String endTime,BasePager basePager){
        PageHelper.startPage(basePager.getPage(), basePager.getRows(), basePager.getSortBy());
        List<DeviceAlertLogPagerVO> list = deviceAlertLogService.findPager(deviceId,startTime,endTime);
        PageInfo<DeviceAlertLogPagerVO> pageInfo = new PageInfo<>(list);
        Map<String, Object> result = new HashMap<>(4);
        result.put("count", pageInfo.getTotal());
        result.put("list", pageInfo.getList());
        return ResultData.success(result);
    }

    @GetMapping("/statistics")
    @ApiOperation(value="web设备告警统计",response = DeviceAlertStatisticsVO.class)
    @ApiImplicitParam(name = "type",value = "1-最近1小时 2-最近24小时 3-最近一周 4-自定义时间")
    public ResultData statistics (String startTime,String endTime,int type){
        return ResultData.success(DeviceAlertStatisticsFactory.create(type).statistics(startTime,endTime,type));
    }

    @GetMapping("/app/pager")
    @ApiOperation(value="app分页查询设备告警记录",response = DeviceAlertLogAppPagerVO.class)
    @ApiImplicitParam(name = "alertType",value="all,other,重量对应属性名")
    public ResultData appPager (BasePager basePager,String alertType,Integer... deviceIds){
        PageHelper.startPage(basePager.getPage(), basePager.getRows(), basePager.getSortBy());
        List<DeviceAlertLogAppPagerVO> list = deviceAlertLogService.findAppPager(alertType,deviceIds);
        PageInfo<DeviceAlertLogAppPagerVO> pageInfo = new PageInfo<>(list);
        Map<String, Object> result = new HashMap<>(4);
        result.put("count", pageInfo.getTotal());
        result.put("list", pageInfo.getList());
        return ResultData.success(result);
    }
}